<?php

class SoutezPresenter extends BasePresenter
{

	public function renderDefault()
	{
		$tym = new TymModel();
		
		$this->template->message = $tym->findById(2)->fetchAll();
	}
	public function renderTabulka($id)
	{
		$soutez_tym = new SoutezModel();
		$zapas = new ZapasModel();
		$tym = new TymModel();
		$this->template->soutez_nazev = $soutez_tym->findById($id)->fetch();
		$tymy = $soutez_tym->tym_soutez_findBySoutezId($id)->fetchAll();
		$result = dibi::query('SELECT t.id AS tym_id,t.nazev,ts.id, ts.plusove_body, ts.minusove_body, z.tym_soutez_id_a, z.tym_soutez_id_b, z.vysledek FROM tym AS t 
			JOIN Tym_Soutez AS ts ON t.id = ts.tym_id
			JOIN Soutez AS s ON ts.soutez_id = s.id
			JOIN Zapas AS z ON ts.id IN (z.tym_soutez_id_a,z.tym_soutez_id_b) 
			WHERE s.id = %i',$id
		)->fetchAll();
		
		$tmp = array();
		foreach($result as $item){
			if(!isset($tmp[$item['id']])){
				$tmp[$item['id']]['nazev'] = '';
				$tmp[$item['id']]['count'] = 0;
				$tmp[$item['id']]['v'] = 0;
				$tmp[$item['id']]['r'] = 0;
				$tmp[$item['id']]['p'] = 0;
				$tmp[$item['id']]['body'] = 0;
				$tmp[$item['id']]['tym_id'] = '';
				$tmp[$item['id']]['plusove_body'] = 0;
				$tmp[$item['id']]['minusove_body'] = 0;
				$tmp[$item['id']]['vysledek'] = '0:0';
			}
			$tmp[$item['id']]['nazev'] = $item['nazev'];
			$tmp[$item['id']]['count'] = $tmp[$item['id']]['count']+1;
			$tmp[$item['id']]['tym_id'] = $item['tym_id'];
			$tmp[$item['id']]['plusove_body'] = $tmp[$item['id']]['plusove_body']+$item['plusove_body'];
			$tmp[$item['id']]['minusove_body'] = $tmp[$item['id']]['minusove_body']+$item['minusove_body'];
			
	
			$tmp_exp_s = explode(':',$tmp[$item['id']]['vysledek']);
			if (isset($item['vysledek']) && $item['vysledek']!='') {
                            $tmp_exp_v = explode(':',$item['vysledek']);
                            $tmp_exp_s[0] = (int)$tmp_exp_s[0];
                            $tmp_exp_s[1] = (int)$tmp_exp_s[1];

                            $tmp_exp_v[0] = (int)$tmp_exp_v[0];
                            $tmp_exp_v[1] = (int)$tmp_exp_v[1];
                            if($item['id'] == $item['tym_soutez_id_a']){
                                    $tmp_exp_s[0] = $tmp_exp_s[0]+$tmp_exp_v[0];
                                    $tmp_exp_s[1] = $tmp_exp_s[1]+$tmp_exp_v[1];
                                    if($tmp_exp_v[0]>$tmp_exp_v[1]){
                                            $tmp[$item['id']]['v'] = $tmp[$item['id']]['v']+1;
                                            $tmp[$item['id']]['body'] = $tmp[$item['id']]['body']+3;
                                    }elseif($tmp_exp_v[0]==$tmp_exp_v[1]){
                                            $tmp[$item['id']]['r'] = $tmp[$item['id']]['r']+1;
                                            $tmp[$item['id']]['body'] = $tmp[$item['id']]['body']+1;
                                    }else{
                                            $tmp[$item['id']]['p'] = $tmp[$item['id']]['p']+1;
                                    }

                            }else{
                                    $tmp_exp_s[0] = $tmp_exp_s[0]+$tmp_exp_v[1];
                                    $tmp_exp_s[1] = $tmp_exp_s[1]+$tmp_exp_v[0];
                                    if($tmp_exp_v[0]<$tmp_exp_v[1]){
                                            $tmp[$item['id']]['v'] = $tmp[$item['id']]['v']+1;
                                            $tmp[$item['id']]['body'] = $tmp[$item['id']]['body']+3;
                                    }elseif($tmp_exp_v[0]==$tmp_exp_v[1]){
                                            $tmp[$item['id']]['r'] = $tmp[$item['id']]['r']+1;
                                            $tmp[$item['id']]['body'] = $tmp[$item['id']]['body']+1;
                                    }else{
                                            $tmp[$item['id']]['p'] = $tmp[$item['id']]['p']+1;
                                    }

                            }
                        }
			
			$tmp[$item['id']]['vysledek'] = $tmp_exp_s[0].':'.$tmp_exp_s[1];
			
			
		}
		$tmp_tmp=array();
		foreach($tmp as $value){
		$tmp_tmp[$value['tym_id']]=$value;	
		}
		/*var_dump($tmp_tmp);
		usort($tmp_tmp,'compare_body');
		var_dump($tmp_tmp);
		$tmp_tmp = array_reverse($tmp_tmp);
		var_dump($tmp_tmp);*/
                $sortarray=array();
		foreach ($tmp_tmp as $val)
                {
                $sortarray[] = $val['body'];
                }
                array_multisort($sortarray,$tmp_tmp);

                //echo '<pre>';
                //print_r($tmp_tmp);
		$tmp = array_reverse($tmp_tmp);
		

		$this->template->tymy = $tmp;
		
	}
	public function renderStatistika($id)
	{
		
	}
	public function renderMenu($id)
	{
		$soutez = new SoutezModel();
		$this->template->soutez_nazev = $soutez->findById($id)->fetch();
		$this->template->id = $id;
	}
}
